#ifndef __ALLOC_H_
#define __ALLOC_H_

#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct block {
    char *begin;        // 内存块起始地址
    char *avail;        // 内存块当前剩余内存起始地址
    char *end;          // 内存块终止地址
    struct block *next; // 下一个内存块地址
} Block;

typedef struct {
    Block *last; // 内存块链表的最末尾
    Block head;  // 内存块链表头结点
} *Heap, HeapEty;
// In C++,  void *  --> int *   is an error
// In C , it is OK  for both  void * --> int *  and  int * --> void *
// In other words, the type checker of C++ is stricter.

#define ALLOC(p)    ((p) = HeapAllocate(MainHeap, sizeof *(p)))
#define ALLOC_CLR(p)   memset(ALLOC(p), 0, sizeof *(p))

#define BLOCK_SIZE 1024

extern Heap MainHeap;

void  InitHeap(Heap hp);
void* HeapAllocate(Heap hp, int size);
void  FreeHeap(Heap hp);
void  DestroyHeap(Heap hp);

#ifdef __cplusplus
}
#endif

#endif
